import express from 'express'
import cors from 'cors'

const app = express()

app.use(cors())
app.use(express.json())

// 简单的登录接口
app.post('/api/auth/login', (req, res) => {
    const { username, password } = req.body
    console.log('收到登录请求:', { username, password })

    if ((username === 'admin' && password === '123456') ||
        (username === 'user' && password === '123456')) {
        res.json({
            success: true,
            message: '登录成功',
            data: {
                token: 'mock-jwt-token-' + Date.now(),
                user: {
                    id: 1,
                    username: username,
                    name: username === 'admin' ? '管理员' : '普通用户',
                    email: `${username}@example.com`,
                    role: username === 'admin' ? 'admin' : 'user'
                }
            }
        })
    } else {
        res.status(401).json({
            success: false,
            message: '用户名或密码错误'
        })
    }
})

// 密码修改接口
app.post('/api/auth/change-password', (req, res) => {
    const { currentPassword, newPassword } = req.body
    const token = req.headers.authorization

    console.log('收到密码修改请求:', { currentPassword, newPassword })

    if (!token) {
        return res.status(401).json({
            success: false,
            message: '未授权'
        })
    }

    if (currentPassword !== '123456') {
        return res.status(400).json({
            success: false,
            message: '当前密码错误'
        })
    }

    if (newPassword.length < 6) {
        return res.status(400).json({
            success: false,
            message: '新密码长度不能少于6位'
        })
    }

    console.log('密码修改成功')
    res.json({
        success: true,
        message: '密码修改成功'
    })
})

// 健康检查接口
app.get('/api/auth/health', (req, res) => {
    res.json({ status: 'OK', message: '后端服务运行正常' })
})

// 获取用户信息
app.get('/api/auth/me', (req, res) => {
    const token = req.headers.authorization
    if (!token) {
        return res.status(401).json({ success: false, message: '未授权' })
    }

    res.json({
        success: true,
        data: {
            user: {
                id: 1,
                username: 'admin',
                name: '管理员',
                email: 'admin@example.com',
                role: 'admin'
            }
        }
    })
})

// 仪表盘统计数据
app.get('/api/dashboard/stats', (req, res) => {
    res.json({
        success: true,
        data: {
            totalUsers: 1560,
            totalProducts: 234,
            todayOrders: 89,
            todayRevenue: 12560
        }
    })
})

// 用户列表
app.get('/api/users', (req, res) => {
    res.json({
        success: true,
        data: [
            { id: 1, name: '张三', role: 'admin', email: 'admin@example.com', status: 'active' },
            { id: 2, name: '李四', role: 'user', email: 'user@example.com', status: 'active' },
            { id: 3, name: '王五', role: 'user', email: 'wangwu@example.com', status: 'inactive' }
        ]
    })
})

// 商品列表
app.get('/api/products', (req, res) => {
    res.json({
        success: true,
        data: [
            { id: 1, name: '商品A', price: 99.9, category: '电子产品', stock: 50, status: 'in_stock' },
            { id: 2, name: '商品B', price: 199.9, category: '家居用品', stock: 0, status: 'out_of_stock' },
            { id: 3, name: '商品C', price: 29.9, category: '图书', stock: 100, status: 'in_stock' }
        ]
    })
})

// 订单列表
app.get('/api/orders', (req, res) => {
    res.json({
        success: true,
        data: [
            { id: 'ORD001', customer: '王五', amount: 299.8, status: 'pending', createTime: '2024-01-01 10:00:00' },
            { id: 'ORD002', customer: '赵六', amount: 99.9, status: 'completed', createTime: '2024-01-01 09:00:00' },
            { id: 'ORD003', customer: '孙七', amount: 159.9, status: 'shipped', createTime: '2024-01-01 08:00:00' }
        ]
    })
})

const PORT = 3001
app.listen(PORT, () => {
    console.log(`✅ 后端服务运行在 http://localhost:${PORT}`)
    console.log(`✅ 健康检查: http://localhost:${PORT}/api/auth/health`)
    console.log('📝 可用接口:')
    console.log('   POST /api/auth/login')
    console.log('   POST /api/auth/change-password')
    console.log('   GET  /api/auth/me')
    console.log('   GET  /api/dashboard/stats')
    console.log('   GET  /api/users')
    console.log('   GET  /api/products')
    console.log('   GET  /api/orders')
})